package com.chixing.day15.TreeMapDemo;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    //根据key排序
    public static void main(String[] args){
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("A",1);
        map.put("B",2);
        map.put("C",3);
        map.put("D",4);
        System.out.println(map);


        //<商品，销量>
        TreeMap<Product, Integer> productMap = new TreeMap<>();
          Product p1 = new Product(1,"背包",400);
          Product p2 = new Product(2,"毛绒玩具",120);
          Product p3 = new Product(3,"文具盒",45.50);


          productMap.put(p1,100);
          productMap.put(p2,100);
          productMap.put(p3,50);
          System.out.println(productMap);

    }
}

//Product 实现了 自然顺序
class Product implements Comparable<Product> {
    private int proId;
    private String proName;
    private double proPrice;

    public Product() {
    }

    public Product(int proId, String proName, double proprice) {
        this.proId = proId;
        this.proName = proName;
        this.proPrice = proprice;
    }

    public int getProId() {
        return proId;
    }

    public void setProId(int proId) {
        this.proId = proId;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public double getProprice() {
        return proPrice;
    }

    public void setProprice(double proprice) {
        this.proPrice = proprice;
    }

    @Override
    public String toString() {
        return "Product{" +
                "proId=" + proId +
                ", proName='" + proName + '\'' +
                ", proprice=" + proPrice +
                '}';
    }

    @Override
    public int compareTo(Product o) {
        return 0;
    }

    //定义比较规则,实现先后顺序的比较
    //先比较name 字典顺序,若name相同,比较价格
    //若价格相同,比较id。

    //1
   /* @Override
    public int compareTo(Product anotherProduct) {
        if (this == anotherProduct){return 0;}
        if (this.proName.compareTo(anotherProduct.proName) == 0) {
            if (this.proPrice == anotherProduct.proPrice) {
                return this.proId - anotherProduct.proId;
            } else {
                  //基本数据类型中没有compareTo方法,需要转换成包装类
                return (Double.valueOf(this.proPrice).compareTo(anotherProduct.proPrice));
            }
        } else {
            return this.proName.compareTo(anotherProduct.proName);
            }
        }
        */

       //2
       TreeMap<Product, Integer> tree = new TreeMap<>(new Comparator<Product>() {
        @Override
        public int compare(Product o1, Product o2) {
            return 0;
        }
    });



}